#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = '云絮'
import requests
import json
from config import config

class sqlmapOpr:
    #adminid=sqlmapadminid
    def __init__(self):
        self.host='http://'+config['sqlmap_addr']+':'+str(config['sqlmap_port'])
    #创建一个新任务
    def taskNew(self):
        url=self.host+'/task/new'
        jsonres=requests.get(url).content
        res=json.loads(jsonres)
        return res['taskid']
    #启动创建的任务扫描
    def taskStart(self,taskid,target,level=1):
        url=self.host+'/scan/'+taskid+'/start'
        jsonres=requests.post(url,data=json.dumps({'url':target,'level':int(level)}),headers={'Content-Type':'application/json'}).content
        #print '测试ID：{taskid},测试等级是:{level}，leve类型：{type}'.format(taskid=taskid,level=level,type=type(level))
    #启动创建的任务扫描,可通过自定义填写json来操控sqlmap参数
    def taskStartByJson(self,taskid,jsonoption):
        url=self.host+'/scan/'+taskid+'/start'
        jsonres=requests.post(url,data=json.dumps(jsonoption),headers={'Content-Type':'application/json'}).content
    #通过任务ID，停止这个任务
    def taskStop(self,taskid):
        url=self.host+'/scan/'+taskid+'/stop'
        jsonres=requests.get(url).content
    #获取扫描后的状态说
    def getStatus(self,taskid):
        try:
            url=self.host+'/scan/'+taskid+'/status'
            jsonres=requests.get(url).content
            res=json.loads(jsonres)
            return res['status']
        except:
            return ""
    #获取扫描后的结果
    def getScanResult(self,taskid):
        url=self.host+'/scan/'+taskid+'/data'
        jsonres=requests.get(url).content
        return jsonres
    #获取进程列表
    # def getScanList(self):
    #     url=self.host+'/admin/'+self.adminid+'/list'
    #     print url
    #     jsonres=json.loads(requests.get(url).content)
    #     return jsonres
#if __name__ == '__main__':
    #丢一个url自动化判断是否有注入

    # api=sqlmapOpr('127.0.0.1',801)
    # taskid=api.taskNew()
    #启动扫描
    # api.taskStart(taskid,'http://127.0.0.1:8088/testmysql.php?id=1')
    #查询状态
    # while 1:
    #     status=api.getStatus(taskid)
    #     if('terminated'==status):
    #         break
    # print api.getScanResult(taskid)

    #列表测试
    #api=sqlmapOpr('127.0.0.1',801)
    #print api.getScanList()

    '''
    >>> r=requests.post("http://127.0.0.1:8011/scan/0bd8c0c0c538bf4b/start",data=jso
n.dumps({'url':'http://localhost:8088/testmysql.php?id=1'}),headers={'Content-Ty
pe':'application/json'})

    '''

